<template>
  <div class="mod-admin-groupProd">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          icon="el-icon-arrow-left"
          type="primary"
          size="small"
          @click="goBackHandle()"
          >{{ $t("live.returnment") }}</el-button
        >
        <el-button
          icon="el-icon-plus"
          type="primary"
          size="small"
          :disabled="status !== 102"
          @click="selectProdHandle()"
          >{{ $t("live.addLicts") }}</el-button
        >
        <span style="color: red"
          >{{ $t("live.numNewLiveProsRem") }}：{{ shopNum }}{{ $t("live.times")
          }}{{ $t("live.subPlatRemLimi") }}{{ totalNum
          }}{{ $t("live.times") }}</span
        >
      </template>
      <template slot-scope="scope" slot="coverPic">
        <img :src="scope.row.coverPic" width="100" height="100" />
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag type="info" v-if="scope.row.status === 2" size="mini">{{
          $t("publics.normal")
        }}</el-tag>
        <el-tag type="warning" v-if="scope.row.status === 4" size="mini">{{
          $t("coupon.illOff")
        }}</el-tag>
        <el-tag type="warning" v-if="scope.row.status === 5" size="mini">{{
          $t("live.platfor")
        }}</el-tag>
      </template>
    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>
    <import-prod
      v-if="importProdVisible"
      ref="importProd"
      @refreshDataList="refreshChange"
    ></import-prod>
    <!-- 商品选择弹窗-->
    <!-- 如果要更改dataUrl，在商品选择组件选择商品后的判断处也要更改一下 -->
    <prods-select
      v-if="selectGroupProdVisible"
      ref="ProdsSelect"
      dataUrl="/group/prod/getNotGroupProdPage"
      @refreshSelectProds="selectGroupProds"
    ></prods-select>
    <!-- 活动商品规格属性设置 -->
    <GroupProdSetting v-if="groupProdSettingVisible" ref="GroupProdSetting">
    </GroupProdSetting>
  </div>
</template>

<script>
import importProd from './liveRoom-import-prod'
import { tableOption } from '@/crud/live/roomProd'

export default {
  data () {
    return {
      dataList: [],
      groupActivity: {},
      selectGroupProdVisible: false,
      groupProdSettingVisible: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataListLoading: false,
      importProdVisible: false,
      shopNum: 0,
      totalNum: 0,
      id: null,
      status: null,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    importProd
  },
  activated () {
    this.id = this.$route.query.id
    this.status = this.$route.query.status
  },
  mounted () {
  },
  methods: {
    // 获取剩余可新增得数量
    getAddRoomNum () {
      this.$http({
        url: this.$http.adornUrl('/live/liveRoomProd/getAddRoomProdNum'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.shopNum = data.shopNum
        this.totalNum = data.totalNum
      })
    },
    getDataList () {
      this.dataForm = {}
      this.id = this.$route.query.id || null
      this.getAddRoomNum()
      this.$http({
        url: this.$http.adornUrl('/live/liveProdStore/pageProdsByRoomId'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: this.pageIndex,
              size: this.pageSize,
              name: this.name ? this.name : null,
              id: this.id ? this.id : null
            }
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
        if (this.selectProds) {
          this.$nextTick(() => {
            this.selectProds.forEach(row => {
              let index = this.dataList.findIndex((prodItem) => prodItem.prodId === row.prodId)
              this.$refs.prodTable.toggleRowSelection(this.dataList[index])
            })
          })
        }
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 返回路由跳转
    goBackHandle () {
      this.$router.go(-1)
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.getDataList(this.page, params)
    },
    // 选择商品操作
    selectProdHandle () {
      this.importProdVisible = true
      this.$nextTick(() => {
        this.$refs.importProd.init(this.id)
      })
    },
    // 选择商品回调
    selectGroupProds (prods) {
      if (prods) {
        // 获取所有的商品
        let prodIds = []
        for (let index = 0; index < prods.length; index++) {
          prodIds.push(prods[index].prodId)
        }
        this.saveGroupProds(prodIds)
      }
    },
    // 保存活动商品
    saveGroupProds (prodIds) {
      this.$http({
        url: this.$http.adornUrl('/group/prod/saveByProdIds'),
        method: 'post',
        data: this.$http.adornData({
          'groupActivityId': this.$route.query.activityId,
          'prodIds': prodIds
        })
      }).then(({ data }) => {
        this.$message({
          message: this.$i18n.t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.getDataList(this.page)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss">
.mod-admin-groupProd {
}
</style>
